--[[
Name: BrownieHelper
Developed by: Sonora (The Dragon Flight)
Website: http://sonora.kirintor.googlepages.com/contents
SVN: http://code.google.com/p/browniehelper/source/browse
License: GNU General Public License v3
]]

function ViewPlayersFrame:Initialize(app)
    --app is the brownie helper app
    
    --Save the brownie helper app
    self.app = app
    
    --Setup a single column table to use for our alias list
    ViewPlayersAliasTable:setFields({'Aliases',})
    
    --Setup a single column table to use as our player selection buttons
    self.col_name = "Players"
    ViewPlayersPlayerTable.candeselect = false
    ViewPlayersPlayerTable:setFields({self.col_name,})
    self:updatePlayerList()
    
    --By default disable the update button
    ViewPlayersUpdateButton:Disable()
    ViewPlayersAddAliasButton:Disable()
    ViewPlayersRemoveAliasButton:Disable()

    --Register a selection handler
    ViewPlayersPlayerTable:registerSelectionCallback(function(row, selected) self:playerSelected(row, selected) end)
    
end

function ViewPlayersFrame:updatePlayerList()

    ViewPlayersPlayerTable:clearRows()
    for player_name, player_info in pairs(self.app:getPlayerDb()) do
        --For each run in the database
        
        ViewPlayersPlayerTable:addRow({
            [self.col_name] = player_name,
            ["player_info"] = player_info,}
        )
    end
    
end

function ViewPlayersFrame:playerSelected(row, selected)

    if selected==true then
        --Setup the right panel for editing
        ViewPlayersClassEditBox:SetText((row.player_info.class or ""))
        ViewPlayersAliasTable:clearRows()
        for i,alias_row in pairs((row.player_info.aliases or {})) do
            ViewPlayersAliasTable:addRow({Aliases=alias_row})
        end
        
        --Enable the update button
        ViewPlayersUpdateButton:Enable()
        ViewPlayersAddAliasButton:Enable()
        ViewPlayersRemoveAliasButton:Enable()
        
    else
        --Setup the right panel for editing
        ViewPlayersClassEditBox:SetText("")
        ViewPlayersAliasTable:clearRows()
        
        --Enable the update button
        ViewPlayersUpdateButton:Disable()
        ViewPlayersRemoveAliasButton:Disable()
        ViewPlayersAddAliasButton:Disable()
        
    end
    
end

--Event handlers
function ViewPlayersFrame:OnAddPlayer()

    local callback = function(response)
        local db = self.app:getPlayerDb()
        db[response] = {}
        self:playerSelected(nil, false)
        self:updatePlayerList()
        self.app:TriggerEvent("BrownieHelper_PlayerDbChanged", response)
    end
    
    inputDialog("Enter a name for the new player:", callback, "")
    
end

function ViewPlayersFrame:OnRemovePlayer()
    
    for i,row in pairs(ViewPlayersPlayerTable:getSelections()) do --should only be one
        callback = function(response)
            if response==true then
                self:playerSelected(row,false)
                self.app:getPlayerDb()[row[self.col_name]] = nil --a delete in lua
                self:updatePlayerList()
                self.app:TriggerEvent("BrownieHelper_PlayerDbChanged", row[self.col_name])
            end
        end
        
        yesnoDialog("Are you sure you want to delete this player entry from the database?  (this will not effect the storage of her point pools)", callback)
        
    end
    
end

function ViewPlayersFrame:OnClose()
    self:Hide()
end

function ViewPlayersFrame:OnAddAlias()
    local callback = function(response)
        --check to make sure there isn't a player already in the database who has this alias
        if self.app:checkAliasesFor(response)~=response then
            messageDialog("We cannot assign the alias '"..response.."' to the current player because another player '"..self.app:checkAliasesFor(response).."' is already using that alias.")
        else ViewPlayersAliasTable:addRow({Aliases=response})
        end
    end
    
    inputDialog("Enter a character name for this player:", callback, "")
    
end

function ViewPlayersFrame:OnRemoveAlias()
    for i,row in pairs(ViewPlayersAliasTable:getSelections()) do
        ViewPlayersAliasTable:deleteRow(row)
    end
end

function ViewPlayersFrame:OnUpdate()
    
    local row = ViewPlayersPlayerTable:getSelections()[1]
    local player_name = row[self.col_name]
    local db = self.app:getPlayerDb()[player_name]
    
    db.class = ViewPlayersClassEditBox:GetText()
    db.aliases = fromValues(ViewPlayersAliasTable:getRows(), 'Aliases')
    
    messageDialog("Player entry updated.")
    self.app:TriggerEvent("BrownieHelper_PlayerDbChanged", player_name)
    
end
